﻿namespace Microsoft.VisualStudio.ServiceModel.DomainServices.Tools
{
    using System;
    using System.Collections.Generic;
    using System.ComponentModel.DataAnnotations;
    using System.Linq;

    internal class BusinessLogicEntity
    {
        private Microsoft.VisualStudio.ServiceModel.DomainServices.Tools.BusinessLogicContext _businessLogicContext;
        private Microsoft.VisualStudio.ServiceModel.DomainServices.Tools.EntityData _entityData;
        private string _name;
        private Type _type;

        public BusinessLogicEntity(Microsoft.VisualStudio.ServiceModel.DomainServices.Tools.BusinessLogicContext businessLogicContext, string name, Type type)
        {
            this._businessLogicContext = businessLogicContext;
            this._name = name;
            this._type = type;
        }

        private IEnumerable<Attribute> GetTypeCustomAttributes()
        {
            List<Attribute> list = this.ClrType.GetCustomAttributes(true).OfType<Attribute>().ToList<Attribute>();
            Type associatedMetadataType = TypeUtilities.GetAssociatedMetadataType(this.ClrType);
            if (associatedMetadataType != null)
            {
                list.AddRange(associatedMetadataType.GetCustomAttributes(true).OfType<Attribute>());
            }
            return list;
        }

        public string AssemblyName
        {
            get
            {
                return this.ClrType.Assembly.GetName().Name;
            }
        }

        public Microsoft.VisualStudio.ServiceModel.DomainServices.Tools.BusinessLogicContext BusinessLogicContext
        {
            get
            {
                return this._businessLogicContext;
            }
        }

        public virtual bool CanBeEdited
        {
            get
            {
                EditableAttribute attribute = this.GetTypeCustomAttributes().OfType<EditableAttribute>().FirstOrDefault<EditableAttribute>();
                return (((attribute == null) || attribute.AllowEdit) && this.CanBeIncluded);
            }
        }

        public virtual bool CanBeIncluded
        {
            get
            {
                return CodeGenUtilities.IsValidGenericTypeParam(this.ClrType);
            }
        }

        public Type ClrType
        {
            get
            {
                return this._type;
            }
        }

        public Microsoft.VisualStudio.ServiceModel.DomainServices.Tools.EntityData EntityData
        {
            get
            {
                if (this._entityData == null)
                {
                    Microsoft.VisualStudio.ServiceModel.DomainServices.Tools.EntityData data = new Microsoft.VisualStudio.ServiceModel.DomainServices.Tools.EntityData {
                        Name = this._name,
                        IsIncluded = false,
                        IsEditable = false,
                        CanBeIncluded = this.CanBeIncluded,
                        CanBeEdited = this.CanBeEdited,
                        AssemblyName = this.AssemblyName
                    };
                    this._entityData = data;
                }
                return this._entityData;
            }
            set
            {
                this._entityData = value;
            }
        }

        public bool IsEditable
        {
            get
            {
                return this.EntityData.IsEditable;
            }
            set
            {
                this.EntityData.IsEditable = value;
            }
        }

        public bool IsIncluded
        {
            get
            {
                return this.EntityData.IsIncluded;
            }
            set
            {
                this.EntityData.IsIncluded = value;
            }
        }

        public string Name
        {
            get
            {
                return this._name;
            }
        }
    }
}

